package com.ymatou.iapi.wxtoken.testcase;

import java.security.MessageDigest;

import org.apache.commons.codec.digest.DigestUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.wxtoken.parameter.GenWxJSignatureBean;
import com.ymatou.iapi.wxtoken.parameter.SyncWxTokenBean;
import com.ymatou.iapi.wxtoken.service.GenWxJSignatureCall;
import com.ymatou.iapi.wxtoken.service.SyncWxTokenCall;
import com.ymt.core.tool.Hex;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.sqlwapper.WXToken2Wapper;
/**
*生成微信签名
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GenWxJSignature{
	private static GenWxJSignatureBean genwxjsignatureBean;
	private static GenWxJSignatureCall genwxjsignatureCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("生成微信签名");
	}

	@Before
	public void caseUp() {
		genwxjsignatureBean=new GenWxJSignatureBean();
		genwxjsignatureCall=new GenWxJSignatureCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(genwxjsignatureCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GenWxJSignature_001() {
		Logger.start(true,"生成微信签名成功");
		try {
			Logger.comment("同步jsapi_ticket");
			SyncWxTokenBean syncwxtokenBean=new SyncWxTokenBean();
			SyncWxTokenCall syncwxtokenCall = new SyncWxTokenCall();
			syncwxtokenBean.setAppId("test");
			syncwxtokenCall.setData(syncwxtokenBean);
			syncwxtokenCall.callService();
			Thread.sleep(2000);
			Logger.comment("生成签名");
			genwxjsignatureBean.setUrl("https://m.ymatou.com/home/ymatou?more=null&money=45.00");
			genwxjsignatureCall.setData(genwxjsignatureBean);
			genwxjsignatureCall.callService();			
			Logger.verifyEquals("200", genwxjsignatureCall.getString("code"), "验证code");
			JsonObject data = genwxjsignatureCall.getJsonObject().get("data").getAsJsonObject();
			Logger.verifyNotNull(data.get("appId"), "appId");
			Logger.verifyNotNull(data.get("signature"), "signature");
			Logger.verifyNotNull(data.get("timeStamp"), "timeStamp");
			Logger.verifyNotNull(data.get("nonceStr"), "nonceStr");
			String tk = new WXToken2Wapper().selectWxtokenByType(1).get(0).get("Token").toString();
			String rdata="jsapi_ticket="+tk
					+ "&noncestr="+data.get("nonceStr").getAsString()
					+ "&timestamp="+data.get("timeStamp").getAsString()
					+ "&url="+genwxjsignatureBean.getUrl();
			Logger.debug(rdata);
			String sign = SignSHA1(rdata);
			Logger.verifyEquals(sign, data.get("signature").getAsString(), "验证sign");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	public static String SignSHA1(String src) throws Exception{
		MessageDigest md = MessageDigest.getInstance("SHA-1");
		md.update(src.getBytes());
		byte[] data = md.digest();
		String datas=Hex.encodeHexStr(data, true);
		return datas;
	}
}